package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Product;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ProductDao {
	private DBHelp<Product> db = new DBHelp<Product>();
	
	public List<Product> findALL() {
		String sql = "SELECT id,prodid,name,price,enable FROM product";
		return db.executeQueryForList(sql, new ProductRowMapper());
	}
	
	public void save(Product product) {
		String sql = "insert into product(prodid,name,price,enable) value(?,?,?,?)";
		db.executeSQL(sql, product.getProdid(),product.getName(),product.getPrice(),product.isEnable());
	}

	public Product finByProdId(String prodid) {
		String sql = "SELECT id,prodid,name,price,enable FROM product WHERE prodid = ?";
		return db.executeQueryForObject(sql, new ProductRowMapper(), prodid);
	}

	public void delete(int id) {
		String sql= "delete from product where id=?";
		db.executeSQL(sql, id);
	}
	
	private class ProductRowMapper implements RowMapper<Product>{

		public Product mapperRow(ResultSet rs) throws SQLException {
			Product product = new Product();
			product.setId(rs.getInt("id"));
			product.setProdid(rs.getString("prodid"));
			product.setName(rs.getString("name"));
			product.setPrice(rs.getFloat("price"));
			product.setEnable(rs.getBoolean("enable"));
			return product;
		}
		
	}

}
